<?php

namespace sixteen;

require_once('sixteen/dom/elements/form.php');

class form extends sixteen::dom::elements::form
{
	function __construct()
	{
		parent::__construct();
	}
	
	function isValid()
	{
		$valid = true;
		foreach($this->children as $field)
		{
			if (!$field->isValid())
			{
				$valid = false;
			}
		}
		return $valid;
	}
	
	function render()
	{
		//self::isValid(); // check validation on render or only on submit?
		return parent::render();
	}
	
	function submit()
	{
		self::setStatesFromPost();
		if (self::isValid())
		{
			echo 'FORM SUBMITTED OK';
			return true;
		}
		else
		{
			return self::render();
		}
	}
	
	function setStatesFromPost()
	{
		if($_POST)
		{
			foreach($this->children as $field)
			{
				@$field->setStateFromValue($_POST[$field->attributes['name']]);
			}
		}
	}
	
}

?>